home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / gs_l2img.ps < prev    next >
Text File  |  1996-01-10  |  6KB  |  170 lines

  1. %    Copyright (C) 1995 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % Emulate the Level 2 dictionary-based image operator in Level 1,
  16. % except for Interpolate (ignored) and MultipleDataSources = true;
  17. % also, we require that the data source be either a procedure of a
  18. % particular form or a stream, not a string or a general procedure.
  19.  
  20. /currentglobal where
  21.  { pop currentglobal { setglobal } true setglobal }
  22.  { { } }
  23. ifelse
  24.  
  25. 7 dict /.csncompdict 1 index def begin
  26.   /DeviceGray { 1 /setgray load } bind def
  27.   /DeviceRGB { 3 /setrgbcolor load } bind def
  28.   /DeviceCMYK { 4 /setcmykcolor load } bind def
  29.   /Indexed
  30.    { dup 1 index 1 get //.csncompdict exch get exec
  31.         % Stack: [/Indexed base hival map] ncomp basesetcolor
  32.      3 -1 roll 3 get mark 3 1 roll
  33.         % Stack: ncomp -mark- basesetcolor map
  34.      dup type /stringtype eq
  35.       { 3 index
  36.      { -
  37.         { exch round cvi get 255 div
  38.         }
  39.        -
  40.         { exch round cvi 3 mul 2 copy 2 copy get 255 div
  41.           3 1 roll 1 add get 255 div
  42.           4 2 roll 2 add get 255 div
  43.         }
  44.         { exch round cvi 4 mul 2 copy 2 copy 2 copy get 255 div
  45.           3 1 roll 1 add get 255 div
  46.           4 2 roll 2 add get 255 div
  47.           5 3 roll 3 add get 255 div
  48.         }
  49.      }
  50.     exch get aload pop counttomark -1 roll
  51.       }
  52.       { /exec load 3 -1 roll
  53.         % -mark- <mapproc> -exec- <basesetcolor>
  54.       }
  55.      ifelse .packtomark cvx
  56.      exch pop 1 exch
  57.    } bind def
  58.   /Separation
  59.    { dup 2 index //.csncompdict exch get exec
  60.         % Stack: [/Separation name alt xform] ncomp altsetcolor
  61.      3 -1 roll 3 get /exec load 3 -1 roll 3 array astore readonly cvx
  62.      exch pop 1 exch
  63.    } bind def
  64.     % Substitute device spaces for CIE spaces.
  65.   /CIEBasedA /DeviceGray load def
  66.   /CIEBasedABC /DeviceRGB load def
  67.   /CIEBasedDEF /DeviceRGB load def
  68.   /CIEBasedDEFG /DeviceCMYK load def
  69. end
  70.  
  71. /.packtomark /packedarray where
  72.  { pop { counttomark packedarray exch pop } }
  73.  { { ] readonly } }
  74. ifelse bind def
  75.  
  76. /.csinextbits        % - .csinextbits <bits>
  77.             % Uses b, nb, i, row, mask, BitsPerComponent;
  78.             % sets b, nb, i.
  79.  { /nb nb
  80.     { dup BitsPerComponent ge { exit } if
  81.       /b b 8 bitshift row i get add def
  82.       /i i 1 add def  8 add
  83.     }
  84.    loop
  85.    BitsPerComponent sub def
  86.    b nb neg bitshift mask and
  87.  } bind def
  88.  
  89. /colorspaceimage    % <width> <height> <bits/comp> <matrix>
  90.             %   <datasrc> <decode> <colorspace> colorspaceimage -
  91.  { save 8 1 roll 20 dict begin
  92.    cvlit /colorspace exch def  cvlit /Decode exch def
  93.    /DataSource exch def  cvlit /ImageMatrix exch def
  94.    /BitsPerComponent exch def  /Height exch def  /Width exch def
  95.    colorspace dup 0 get .csncompdict exch get exec
  96.      /setpixelcolor exch def  /ncomp exch def  pop
  97.    /bpp ncomp BitsPerComponent mul def
  98.    /row bpp Width mul 7 add 8 idiv string def
  99.    languagelevel 2 gt    %****** ge
  100.     { colorspace setcolorspace /ImageType 1 def
  101.       currentdict image
  102.     }
  103.     { /mask 1 BitsPerComponent bitshift 1 sub def
  104.       /nextbits BitsPerComponent 8 eq
  105.        { mark row { i get /i i 1 add def } aload pop .packtomark cvx }
  106.        { /.csinextbits load }
  107.       ifelse def
  108.       /nextpixel mark 0 2 ncomp 1 sub 2 mul
  109.        { /nextbits cvx exch
  110.      Decode exch 2 getinterval
  111.      dup aload pop exch sub
  112.      dup mask eq { pop } { mask div /mul load 3 -1 roll } ifelse
  113.      0 get dup 0 eq { pop } { /sub load 3 -1 roll } ifelse
  114.        }
  115.       for
  116.       /setpixelcolor load dup type /operatortype ne { /exec load } if
  117.       .packtomark cvx def
  118.       /readrow { DataSource row readstring pop pop }
  119.         /DataSource load dup type
  120.         dup /arraytype eq exch /packedarraytype eq or
  121.     {    % Must be { <file> <string> readhexstring pop }
  122.       0 get row /readhexstring load /pop load
  123.       4 array astore cvx exch pop
  124.     }
  125.     { pop
  126.     }
  127.       ifelse def
  128.       ImageMatrix matrix invertmatrix concat
  129.       0 1 Height 1 sub
  130.        { gsave 0 exch translate
  131.      readrow
  132.      /b 0 def  /nb 0 def  /i 0 def
  133.      Width
  134.       { nextpixel
  135.         1 1 true {1 0 0 1 0 0} {<80>} imagemask
  136.         1 0 translate
  137.       }
  138.      repeat grestore
  139.        }
  140.       for
  141.     }
  142.    ifelse end restore
  143.  } bind def
  144.  
  145. % Patch for testing.
  146. /.cincompdict 3 dict begin
  147.   1 { {0 1} {/DeviceGray} } def
  148.   3 { {0 1 0 1 0 1} {/DeviceRGB} } def
  149.   4 { {0 1 0 1 0 1 0 1} {/DeviceCMYK} } def
  150. currentdict end def
  151. /colorimage        % <width> <height> <bits/comp> <matrix>
  152.             %   <datasrc> false <ncomp> colorimage -
  153.  { 1 index { /colorimage load /rangecheck signalerror } if exch pop
  154.    //.cincompdict exch get exec colorspaceimage
  155.  } bind odef
  156. /image
  157.  { dup type /dicttype ne
  158.     { //image }
  159.     { begin /Width load /Height load /BitsPerComponent load
  160.       /ImageMatrix load /DataSource load /Decode load currentcolorspace
  161.       end colorspaceimage
  162.     }
  163.    ifelse
  164.  } bind odef
  165.  
  166. exec
  167.